import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
      isPlay: true,
      songCur:-1,
      songList:[],
      //当前播放时间
      songCurTime:0,
      //想要设置的时间
      ssongCurTime:0,
      songTime:0,
      user:null
    },
    mutations: {
      setIsPlay(state, isPlay){
        console.log('isPlay:',isPlay)
        state.isPlay = isPlay
      },
      setSongCur(state, cur){
        console.log('cur:',cur)
          state.songCur = cur
      },
      pushSongList(state,song){
        let cur = 0;
        if(state.songCur != -1) cur = state.songCur+1;
        state.songCur = cur;
        state.songList.splice(state.songCur,0,song);
      },
      popSongList(state,index){
        console.log("index....",index)
        if(index < state.songCur) state.songCur-=1;
        state.songList.splice(index,1);
      },
      setSongList(state, songList){
        console.log('songlist:', songList)
        state.songList = songList
      },
      setSongCurTime(state, songCurTime){
        // console.log('curTime-->',songCurTime)
        state.songCurTime = songCurTime
      },
      setSsongCurTime(state, ssongCurTime){
        state.ssongCurTime = ssongCurTime
      },
      setSongTime(state, songTime){
        console.log('songTime:', songTime)
        state.songTime = songTime
      },
      setUser(state, user){
        sessionStorage.setItem('user',JSON.stringify(user));
        state.user = user;
      }
    },
    getters:{
        getIsPlay : state => state.isPlay,
        getSongCur : state => state.songCur,
        getSongList : state => state.songList,
        getSongCurTime : state => state.songCurTime,
        getSsongCurTime : state => state.ssongCurTime,
        getSongTime : state => state.songTime,
        getUser(state){
          if(state.user != null){
            return state.user;
          }
          if(state.user == null && sessionStorage.getItem('user') != null){
            state.user = JSON.parse(sessionStorage.getItem('user'));
            return state.user;
          }else{
            return null;
          }
        }
    }
});

export default store;